package com.huatai.datacenter.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huatai.common.api.R;
import com.huatai.common.httputil.HttpRequest;
import com.huatai.datacenter.entity.postdata.HttpRequestEntity;
import com.huatai.datacenter.service.HttpRequestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Lion
 * @date 2023/6/14  19:55
 */
@Slf4j
@RequestMapping("/httpRequest")
@RestController
@Api(value = "HTTP接入", tags = "HTTP接入")
public class HttpRequestController extends BaseController<HttpRequestService, HttpRequestEntity> {

	/**
	 * @author Lion
	 * @description 添加http请求调度任务，并注册到调度任务中
	 * @param data
	 * @return com.huatai.common.api.R
	 */
	@ApiOperation("新增HTTP接入")
	@Override
	public R save(@RequestBody HttpRequestEntity data) {

		return R.success(super.service.saveRequestData(data));
	}

	@ApiOperation("更新")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "HttpRequestEntity", value = "HttpRequestEntity",type = "HttpRequestEntity")
	})
	@PostMapping("/update")
	public R updataData(@RequestBody HttpRequestEntity data){

		return R.success(super.service.updateRequestData(data));
	}

	/**
	 * @author Lion
	 * @description 启动调度任务
	 * @param data
	 * @return com.huatai.common.api.R
	 */
	@ApiOperation("启动")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "jobId", value = "任务id",type = "String")
	})
	@PostMapping("/start")
	public R startJob(@RequestBody JSONObject data) {

		return R.success(super.service.startJob(data));
	}

	/**
	 * @author Lion
	 * @description 停止调度任务
	 * @param  data
	 * @return com.huatai.common.api.R
	 */
	@ApiOperation("停止")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "jobId", value = "任务id",type = "String")
	})
	@PostMapping("/stop")
	public R pause(@RequestBody JSONObject data) {

		return R.success(super.service.stopJob(data));
	}

	/**
	 * @author Lion
	 * @description 删除调度任务
	 * @param data
	 * @return com.huatai.common.api.R
	 */
	@ApiOperation("删除")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "data", value = "id数组",type = "List")
	})
	@Override
	public R delete(@RequestBody List<String> data) {

		this.service.removeByIds(data);
		String resultData = this.service.removeData(data);

		return R.success(resultData);
	}

	/**
	 * @author Lion
	 * @description 执行一次
	 * @param  data
	 * @return com.huatai.common.api.R
	 */
	@ApiOperation("执行一次")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "jobId", value = "任务id",type = "Stirng")
	})
	@PostMapping("/trigger")
	public R triggerJob(@RequestBody JSONObject data) {

		String jobId = data.get("jobId").toString();
		String resultData = this.service.triggerJob(jobId);

		return R.success(resultData);
	}

	@ApiOperation("分页获取日志")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "jobId", value = "任务id",dataType = "integer"),
		@ApiImplicitParam(name = "pageSize", value = "分页条数",dataType = "integer"),
		@ApiImplicitParam(name = "pageIndex", value = "当前页",dataType = "integer")
	})
	@PostMapping("/pageList")
	public R pageTaskLog(@RequestBody JSONObject data){

		String result = this.service.pageTaskJob(data);

		return R.data(JSONObject.parseObject(result));
	}

	//获取所有每个http接入任务成功率比上失败率
	@ApiOperation("获取所有每个http接入任务成功率比上失败率")
	@PostMapping("/queryLogData")
	public R queryLogData(){

		String result = this.service.queryData();
		return R.data(JSONArray.parseArray(result));
	}
}
